package com.mcptt.http.operator;

import com.cloudptt.api.product.log.Log;
import com.mcptt.http.operator.OkHttpUtils;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

public class HttpOperator {
	private static final int ConnectionTimeout = 5 * 1000;
	private static final int SoTimeout = 5 * 1000;

	public Response httpGetRequest(String url, Map<String, String> params)
			throws Exception {
		Log.debug(getClass(), "httpGetRequest url = " + url + " params = " + params);
		StringBuilder sb = new StringBuilder();
		sb.append(url).append("?");
		if (params != null && params.size() != 0) {
			for (Map.Entry<String, String> entry : params.entrySet()) {
				// 如果请求参数中有中文，需要进行URLEncoder编码 gbk/utf8
				sb.append(entry.getKey()).append("=");
				if(!entry.getKey().equals("code_challenge") && !entry.getKey().equals("scope")) {
//					sb.append(URLEncoder.encode(entry.getValue(), "utf-8"));
					sb.append(entry.getValue());
				}
				else
				{
					sb.append(entry.getValue());
				}
				sb.append("&");
			}
			sb.deleteCharAt(sb.length() - 1);
		}
		return OkHttpUtils.getInstance().doGet(sb.toString());
	}
//	public HttpResponse httpGetRequest(String url, Map<String, String> params)
//			throws Exception {
//		StringBuilder sb = new StringBuilder();
//		sb.append(url).append("?");
//		if (params != null && params.size() != 0) {
//			for (Map.Entry<String, String> entry : params.entrySet()) {
//				// 如果请求参数中有中文，需要进行URLEncoder编码 gbk/utf8
//				sb.append(entry.getKey()).append("=");
//				sb.append(URLEncoder.encode(entry.getValue(), "utf-8"));
//				sb.append("&");
//			}
//			sb.deleteCharAt(sb.length() - 1);
//		}
//
//		DefaultHttpClient httpCient = new MyHttpClient().getHttpClient(
//				ConnectionTimeout, SoTimeout);
//		HttpGet httpGet = new HttpGet(sb.toString());
//		httpCient.setRedirectHandler(new RedirectHandler());
//		return httpCient.execute(httpGet);
//	}

	public Response httpGetRequest(String url) throws Exception {
		Log.debug(getClass(), "httpGetRequest url = " + url);
//		HttpGet httpGet = new HttpGet(url);
//		DefaultHttpClient httpCient = new MyHttpClient().getHttpClient(
//				ConnectionTimeout, SoTimeout);
//		httpCient.setRedirectHandler(new RedirectHandler());
//
//		return httpCient.execute(httpGet);
		return OkHttpUtils.getInstance().doGet(url);
	}

	public Response httpGetRequestByAccessToken(String url,
													String accessToken) throws Exception {
//		StringBuilder sb = new StringBuilder();
//		sb.append(url);
//
//		DefaultHttpClient httpCient = new MyHttpClient().getHttpClient(
//				ConnectionTimeout, SoTimeout);
//		HttpGet httpGet = new HttpGet(sb.toString());
//		httpGet.addHeader("Authorization", "Bearer " + accessToken);
//		httpCient.setRedirectHandler(new RedirectHandler());
//		return httpCient.execute(httpGet);
		Map<String,String> headerMap = new HashMap<String,String>();
		headerMap.put("Authorization","Bearer " +accessToken);
		return OkHttpUtils.getInstance().doGet(url, headerMap);
	}
//	public HttpResponse httpGetRequestByAccessToken(String url,
//			String accessToken) throws Exception {
//		StringBuilder sb = new StringBuilder();
//		sb.append(url);
//
//		DefaultHttpClient httpCient = new MyHttpClient().getHttpClient(
//				ConnectionTimeout, SoTimeout);
//		HttpGet httpGet = new HttpGet(sb.toString());
//		httpGet.addHeader("Authorization", "Bearer " + accessToken);
//		httpCient.setRedirectHandler(new RedirectHandler());
//		return httpCient.execute(httpGet);
//	}
public Response httpGetRequest(String url, String cookie)
		throws Exception {
//	HttpGet httpGet = new HttpGet(url);
//	httpGet.addHeader("Cookie", cookie);
//	DefaultHttpClient httpCient = new MyHttpClient().getHttpClient(
//			ConnectionTimeout, SoTimeout);
//	httpCient.setRedirectHandler(new RedirectHandler());
//
//	return httpCient.execute(httpGet);
	Map<String,String> headerMap = new HashMap<String,String>();
	headerMap.put("Cookie",cookie);
	return OkHttpUtils.getInstance().doGet(url,headerMap);
}
//	public HttpResponse httpGetRequest(String url, String cookie)
//			throws Exception {
//		HttpGet httpGet = new HttpGet(url);
//		httpGet.addHeader("Cookie", cookie);
//		DefaultHttpClient httpCient = new MyHttpClient().getHttpClient(
//				ConnectionTimeout, SoTimeout);
//		httpCient.setRedirectHandler(new RedirectHandler());
//
//		return httpCient.execute(httpGet);
//	}

	public Response httpGetRequest(String url, String cookie,
			String authDscp ) throws Exception {
//		HttpGet httpGet = new HttpGet(url);
//		httpGet.addHeader("Cookie", cookie);
//		httpGet.addHeader("Authorization", authDscp);
//		DefaultHttpClient httpCient = new MyHttpClient().getHttpClient(
//				ConnectionTimeout, SoTimeout);
//		httpCient.setRedirectHandler(new RedirectHandler());
//
//		return httpCient.execute(httpGet);
		Map<String,String> headerMap = new HashMap<String,String>();
		headerMap.put("Cookie",cookie);
		headerMap.put("Authorization", authDscp);
		return OkHttpUtils.getInstance().doGet(url,headerMap);
	}

//	public class RedirectHandler extends DefaultRedirectHandler {
//		@Override
//		public boolean isRedirectRequested(HttpResponse response,
//				HttpContext context) {
//			// TODO Auto-generated method stub
//			return false;
//		}
//	}

//	public class MyHttpClient extends DefaultHttpClient {
//		/**
//		 * 获取 HttpClient,主要是封装了超时设置
//		 *
//		 * @param rTimeOut
//		 *            请求超时
//		 * @param sTimeOut
//		 *            等待数据超时
//		 * @return
//		 */
//		public DefaultHttpClient getHttpClient(int rTimeOut, int sTimeOut) {
//			BasicHttpParams httpParams = new BasicHttpParams();
//			HttpConnectionParams.setConnectionTimeout(httpParams, rTimeOut);
//			HttpConnectionParams.setSoTimeout(httpParams, sTimeOut);
//			DefaultHttpClient client = new DefaultHttpClient(httpParams);
//			return client;
//		}
//	}

//	public HttpResponse httpPostRequest(String url, List<NameValuePair> params)
//			throws Exception {
//		HttpPost httpRequest = new HttpPost(url);
//		httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//		DefaultHttpClient httpCient = new MyHttpClient().getHttpClient(
//				ConnectionTimeout, SoTimeout);
//
//		return httpCient.execute(httpRequest);
//	}

	public Response httpPostRequest(String url, String cookie, Map<String, String> params)
			throws Exception {
//		HttpPost httpRequest = new HttpPost(url);
//		httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//		DefaultHttpClient httpCient = new MyHttpClient().getHttpClient(
//				ConnectionTimeout, SoTimeout);
//
//		return httpCient.execute(httpRequest);
		return OkHttpUtils.getInstance().doPost(url,cookie,params);
	}

	public Response httpPostRequest(String url, Map<String, String> params)
			throws Exception {
//		HttpPost httpRequest = new HttpPost(url);
//		httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//		DefaultHttpClient httpCient = new MyHttpClient().getHttpClient(
//				ConnectionTimeout, SoTimeout);
//
//		return httpCient.execute(httpRequest);
		return OkHttpUtils.getInstance().doPost(url,params);
	}
}
